package com.cetool.autocms.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.cetool.autocms.common.PageRequest;
import com.cetool.autocms.enumeration.OperateEnum;
import com.cetool.autocms.exception.ServiceException;
import com.cetool.autocms.mapper.user.UserMapper;
import com.cetool.autocms.pojo.domain.user.UserInfoDO;
import com.cetool.autocms.pojo.dto.user.UserInfoDTO;
import com.cetool.autocms.pojo.param.user.QueryUserListParam;
import com.cetool.autocms.pojo.param.user.SaveUserParam;
import com.cetool.autocms.pojo.param.user.UpdateUserParam;
import com.cetool.autocms.pojo.request.user.DeleteUserRequest;
import com.cetool.autocms.pojo.request.user.QueryUserListRequest;
import com.cetool.autocms.pojo.request.user.SaveUserRequest;
import com.cetool.autocms.pojo.request.user.UpdateUserRequest;
import com.cetool.autocms.service.enumeration.UserExceptionEnum;
import com.cetool.autocms.service.user.UserService;
import com.cetool.autocms.util.BeansUtil;
import com.github.pagehelper.PageSerializable;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

/**
 * 用户信息服务实现
 *
 * @author 戴越
 * @date 2021/11/9
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    /**
     * 注入用户管理持久层
     */
    @Autowired
    private UserMapper userMapper;

    @Override
    public OperateEnum saveUser(SaveUserRequest request) {
        SaveUserParam param = new SaveUserParam();
        BeanUtil.copyProperties(request, param);
        param.setUserPassWord(DigestUtils.md5DigestAsHex((param.getUserName() + param.getUserPassWord()).getBytes()).toUpperCase());
        if (userMapper.queryUserIsExist(param) > 0) {
            throw new ServiceException(UserExceptionEnum.CREATE_USER_INFO_FAILED);
        }
        userMapper.saveUser(param);
        return OperateEnum.ADD_SUCCESS;
    }

    @Override
    public PageSerializable<UserInfoDTO> listPageUser(PageRequest<QueryUserListRequest> request) {
        QueryUserListParam param = new QueryUserListParam();
        BeanUtil.copyProperties(request.getData(), param);
        PageMethod.startPage(request.getPageNum(), request.getPageSize());
        return BeansUtil.pageCopy(new PageSerializable<>(userMapper.listUser(param)), UserInfoDTO.class);
    }

    @Override
    public OperateEnum deleteUser(DeleteUserRequest request) {
        Integer userId = request.getUserId();
        //删除管理员用户
        //查询管理员用户是否存在
        UserInfoDO userInfo = userMapper.getUser(userId);
        if (ObjectUtil.isNull(userInfo)) {
            throw new ServiceException(UserExceptionEnum.CHECK_USER_NO_EXIST);
        }
        userMapper.deleteUser(userId);
        return OperateEnum.DELETE_SUCCESS;
    }

    @Override
    public OperateEnum updateUser(UpdateUserRequest request) {
        UpdateUserParam param = new UpdateUserParam();
        BeanUtil.copyProperties(request, param);
        if (!StringUtils.isEmpty(param.getUserPassWord())){
            param.setUserPassWord(DigestUtils.md5DigestAsHex((param.getUserName() + param.getUserPassWord()).getBytes()).toUpperCase());
        }
        userMapper.updateUser(param);
        return OperateEnum.UPDATE_SUCCESS;
    }

}
